﻿/*=====================================================================================
/*--------------------------------------------------
 OIL MODULE JAVASCRIPT FILE
 Author : Finance Google Gadget Team Oil/Gas Group 
----------------------------------------------------*/



//constant that corresponds to oil tab
var OIL_TAB_ID = "oil_tab";

//Object that contains all usefull information about current oil type
function OilInfo() {
		//The time, which this information is dated  
		this.date; 
		//The name of the current type of the oil                    
		this.name;
		//Price (dollars for barrel)
		this.price;
		//Dynamic (compared whith previous day) in valute                
		this.valuteDynamic;
		//Dynamic (compared whith previous day) in percent          
		this.percentDynamic;     
}
//------------------------------------------------------------------------------------

//FUNCTIONS THAT PROCESS DAILY OIL INFORMATION

//Function that displays parsed Oil information		
function getOilTable (oilvalues){
	//Table headline
	var tablehead = "<thead><tr><td>" + prefs.getMsg("oil_type") +"</td><td>" +  prefs.getMsg("date") +"</td><td>"+ prefs.getMsg("oil_price") +"</td><td>"+ prefs.getMsg("dyn_val") +"</td><td>"+ prefs.getMsg("perc_change") + "</td></tr></thead>"; 
	var tablebody = "<tbody>";
	for (var i = 0; i < oilvalues.length; i++){
			//Turning oilvalues[i].date into the dd.mm format
			correctDate  = oilvalues[i].date.substring(0, 2) +"."+ oilvalues[i].date.substring(2, 4); 	
			tablebody += "<tr " + ((i % 2 == 1) ? "class = 'even'>" : "class = 'odd' >");
			tablebody += "<td><a href='#' title='" + prefs.getMsg("dynamic") + ":&nbsp;"+prefs.getMsg(oilvalues[i].name)+"' onclick='ReportLink_OnClick(\""+ oilvalues[i].name + "\",\"" + oilvalues[i].date + "\",this.title); return false;'><b>" + prefs.getMsg(oilvalues[i].name) + "</b></a></td>";
			tablebody += "<td align=center>" + correctDate + "</td>";
			tablebody += "</td><td align=right><b>" + formatValueToRegStd(oilvalues[i].price) + "<b></td>";
			if (oilvalues[i].valuteDynamic <= "0"){
			tablebody +=	"<td align=right class = 'decrease'>" + formatValueToRegStd(oilvalues[i].valuteDynamic) + "</td><td align=right class ='decrease'>" + formatValueToRegStd(oilvalues[i].percentDynamic) + "</td></tr>";
			}
		else{
			tablebody +=	"<td align=right class = 'increase'>+" + formatValueToRegStd(oilvalues[i].valuteDynamic) + "</td><td align=right class ='increase'>+" + formatValueToRegStd(oilvalues[i].percentDynamic) + "</td></tr>";
		}
	}
	tablebody+="</tbody>";
	var html  =  "<table class = 'rate_table'>" + tablehead + tablebody + "</table>";		  
		_gel(OIL_TAB_ID).innerHTML = html;
	_IG_AdjustIFrameHeight();
}
//-------------------------------------------------------------------------------------



//	Function that parses XMl document whith oil information
function parseDisplayOilXmlInfo (response){ 
	//Error processing
	if (responseError(response, OIL_TAB_ID)) {
		return;
	}
		var oilvalues = new Array();
		//Examining of the "Row" section 
		var oilTypeRow = response.getElementsByTagName("Row");
		for (var i = 0; i < oilTypeRow.length ; i++) {
				//New oil type cell creating 
				oilvalues[i] = new OilInfo();
				//Loop throw "Row" child nodes  
				var oilTypeList = oilTypeRow.item(i).childNodes;
				for (var j = 0; j < oilTypeList.length ; j++) {
					//Getting information for current oil type     
					var currentOilType = oilTypeList.item(j);
					//Infilling oilvalues[i] fields     
					if (currentOilType.attributes.item(0).nodeValue == "Date") {
									//Putting the date into correct form (javascript object Date())        
									var currentDate  =  currentOilType.firstChild.nodeValue;
									oilvalues[i].date = currentDate.substring(0, 2) + currentDate.substring(3, 5) + currentDate.substring(6, 10);            
					}
					if (currentOilType.attributes.item(0).nodeValue == "Ticker") {
									oilvalues[i].name  = currentOilType.firstChild.nodeValue;
									continue;
					}
					if (currentOilType.attributes.item(0).nodeValue == "Close") {
									oilvalues[i].price  = normalizeFloat(replaceCommaWithDot(currentOilType.firstChild.nodeValue), 2);
									continue;
					}
					if (currentOilType.attributes.item(0).nodeValue == "ChCl") {
									oilvalues[i].valuteDynamic  = normalizeFloat(replaceCommaWithDot(currentOilType.firstChild.nodeValue), 2);
									continue;
					}
					if (currentOilType.attributes.item(0).nodeValue == "ChClProc") {
									oilvalues[i].percentDynamic  = normalizeFloat(replaceCommaWithDot(currentOilType.firstChild.nodeValue), 2);
									continue;
					}
				}
		}
						
	getOilTable(oilvalues);
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------


//FUNCTIONS THAT PROCESS OIL DYNAMIC INFORMATION


//Function that forms an array of dinamic information of one of available oil types
function parseOilDynamic (response, title){
	//Error processing
	if (responseError(response, OIL_TAB_ID)) {
		return;
	}
		//Examining of the "Row" section 
		var oilTypeRow = response.getElementsByTagName("Row");
		var oilTypeRowLength = oilTypeRow.length;
			var oilDyn = new Array(oilTypeRowLength);
		for (var i = 0; i < oilTypeRowLength ; i++) {
		//New oil type cell creating 
		oilDyn[oilTypeRowLength  - i - 1] = new RateDynamic();
			//Loop throw "Row" child nodes  
			var oilTypeList = oilTypeRow.item(i).childNodes;
			for (var j = 0; j < oilTypeList.length ; j++) {
					//Getting information for current oil type     
					var currentOilType = oilTypeList.item(j);
					//Infilling oilDyn[i] fields     
					if (currentOilType.attributes.item(0).nodeValue == "Date") {
						var currentDate  =  currentOilType.firstChild.nodeValue;
						//Turning oilvalues[i].date into the dd.mm.yyyy format
						var correctDate  = currentDate.substring(0, 2) +"."+ currentDate.substring(3, 5) + "." + currentDate.substring(6, 10); 
						oilDyn[oilTypeRowLength  - i - 1].date = correctDate;
						continue;
					} 
					if (currentOilType.attributes.item(0).nodeValue == "Close") {
						oilDyn[oilTypeRowLength  - i - 1].value  = normalizeFloat(replaceCommaWithDot(currentOilType.firstChild.nodeValue), 2);
						continue;
					}
			} 
		}  						 	         
	//This function will initialize pop-up window, create table and graph when we retreive currencies dynamic from XML
	CreateReportWindow(oilDyn, title);
	
	
}
//-------------------------------------------------------------------------------------





//Function that handles onclick event of initialize pop-up window link
function ReportLink_OnClick(oilTypeName, stringCurrentDate, title) { 
		currentDate = new Date(stringCurrentDate.substring(4, 8), stringCurrentDate.substring(2, 4) - 1, stringCurrentDate.substring(0, 2));
		var url  = "http://www.prime-tass.ru/results/export.asp?funcNum=84&format=xml&code="+ oilTypeName +"&sDate="+ getDateForNDaysAgo(currentDate, RATE_PERIOD).getTime() +"&eDate=" + currentDate.getTime();
 		_IG_FetchXmlContent(url, _IG_Callback(parseOilDynamic, title));
}

//------------------------------------------------------------------------------------- 
//-------------------------------------------------------------------------------------



// MAIN FUNCTION
function displayOilInfo(){
		showLoadingInfo(OIL_TAB_ID);
		var today  = new Date(); 
		var url = "http://www.prime-tass.ru/results/export.asp?funcNum=84&format=xml&code=All&sDate=1174338000000&eDate=" + today.getTime();
		_IG_FetchXmlContent(url, parseDisplayOilXmlInfo);
}

//=====================================================================================


